//
//  MyOrderedViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/5/23.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "MyOrderedViewController.h"
#import "StudentModel.h"
#import "StudentCell.h"
#import "UserInfoModel.h"
#import "StudentInfoController.h"

@interface MyOrderedViewController () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation MyOrderedViewController

#pragma mark - 初始化方法
-(instancetype)init{
    if (self==[super init]) {
        // 1.设置标题&TabBar图标
        self.title = @"我的预约";
        self.tabBarItem.image = [UIImage imageNamed:@"news_tabBar"];
        self.hidesBottomBarWhenPushed = false;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化tableView
    [self setupTableView];
}

-(void)setupTableView {
    // 设置TableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    // 3.网络获取数据
    
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 3.1 获取活动数据
        [self updateStudentDateLastId:0 getCount:6];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 获取活动数据
        StudentModel *model = self.dataArray.lastObject;
        [self updateStudentDateLastId:model.sid getCount:6];
    }];
}

// 获取活动数据
-(void)updateStudentDateLastId:(NSInteger)lastId getCount:(NSInteger)count {
    // 判断是否(下拉刷新)
    if (lastId == 0) {
        // 下拉刷新,则清空数组
        self.dataArray = [[NSMutableArray alloc] init];
    }
    
    // 初始化参数
    NSDictionary *paramters = @{@"tid": [UserInfoModel shareInstance].tid,@"token": [UserInfoModel shareInstance].token, @"last_id": @(lastId), @"count": @(count)};
    
    [[NetworkTool shareInstance] requireMethodType:POSTType URLString: @"SDSY/TgetYuyuelaoshi" parameters: paramters success:^(NSDictionary *respondDictionary) {
        // 1.储存数据
        for (NSDictionary *dic in respondDictionary[@"studentsOrderList"]) {
            StudentModel *model = [[StudentModel alloc] initWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        
        // 2.检查是否正常加载
        if ([respondDictionary[@"studentsOrderList"] count] < count) {
            // 没有更多数据
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            // 正常结束刷新
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [self updateFailed];
    }];
}

// 如果请求失败,HUD显示错误,tableVIew刷新空数据,foot提示没有数据
- (void)updateFailed {
    [SVProgressHUD showfailed];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

#pragma mark - tableView回调函数

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    // 取出模型
    StudentModel *model = self.dataArray[indexPath.row];
    
    // 显示活动详情页
    StudentInfoController *controller = [[StudentInfoController alloc] init];
    controller.sid = model.sid;
    [self.navigationController pushViewController:controller animated:true];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (cell==nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"StudentCell" owner:nil options:nil].firstObject;
        
    }
    
    StudentModel *model = self.dataArray[indexPath.row];
    
    cell.model = model;
    cell.indexPath = indexPath;
    return cell;
}


@end
